package edu.isquen.core.document.model;

import java.io.Serializable;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import edu.isquen.core.base.model.ValueObject;
import edu.isquen.core.form.model.Form;
import edu.isquen.core.form.service.FormService;
import edu.isquen.core.user.model.User;
import edu.isquen.util.factory.ServiceFactory;

public class Document extends ValueObject implements Serializable{

	private static final long serialVersionUID = 1911437033491753695L;

	private String id;
	
	private String name;
	
	private String formName;
	
	private String formId;
	
	private String description;
	
	private User author;
	
	private Date createDate;
	
	private User lastModifyUser;
	
	private Date lastModifyDate;
	
	private boolean editAble;
	
	private Map<String, Item> _items = new HashMap<String, Item>();
	
	public void addItems(Collection<Item> items) {
		if (items != null && items.size() > 0) {
			Iterator<Item> iters = items.iterator();
			while (iters.hasNext()) {
				Item item = (Item) iters.next();
				if (item != null) {
					addItem(item);
				}
			}
		}
	}
	
	public void addItem(Item item){
		_items.put(item.getName().toUpperCase(), item);
	}
	
	public Collection<Item> getItems() {
		return _items.values();
	}
	
	public void removeItem(Item item) {
		if (item != null) {
			_items.remove(item.getName());
		}
	}
	
	public Item findItem(String fieldname) {
		if (fieldname != null) {
			Item item = (Item) _items.get(fieldname.toUpperCase());
			return item;
		}

		return null;
	}

	
	public Form getForm() throws Exception {
		FormService process = (FormService) ServiceFactory.createProcess(FormService.class);
		Form form = (Form) process.doView(this.getFormId());

		return form;
	}
	
	public Map<String, Item> get_items() {
		return _items;
	}

	public void set_items(Map<String, Item> items) {
		_items = items;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getFormName() {
		return formName;
	}

	public void setFormName(String formName) {
		this.formName = formName;
	}

	public String getFormId() {
		return formId;
	}

	public void setFormId(String formId) {
		this.formId = formId;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public User getAuthor() {
		return author;
	}

	public void setAuthor(User author) {
		this.author = author;
	}

	public Date getCreateDate() {
		return createDate;
	}

	public void setCreateDate(Date createDate) {
		this.createDate = createDate;
	}

	public User getLastModifyUser() {
		return lastModifyUser;
	}

	public void setLastModifyUser(User lastModifyUser) {
		this.lastModifyUser = lastModifyUser;
	}

	public Date getLastModifyDate() {
		return lastModifyDate;
	}

	public void setLastModifyDate(Date lastModifyDate) {
		this.lastModifyDate = lastModifyDate;
	}

	public boolean getEditAble() {
		return editAble;
	}

	public void setEditAble(boolean isEditAble) {
		this.editAble = isEditAble;
	}
	
}
